package com.open.capacity.user.controller;

import java.util.Map;

import com.open.capacity.commons.Result;
import com.open.capacity.user.model.SysConfig;
import com.open.capacity.user.service.SysConfigService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * @author
 * @email
 * @date 2021-08-18 11:05:17
 */
@RestController
@RequestMapping("sysconfigs")
public class SysConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('sys_config:sysconfig:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = sysConfigService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findByKey")
    @PreAuthorize("hasAnyAuthority('sys_config:sysconfig:list')")
    public Result findByKey(String key) {
        return Result.succeed(sysConfigService.findByKey(key));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('sys_config:sysconfig:save')")
    public Result save(@RequestBody @Valid SysConfig sysConfig, String keyValue) {
        sysConfigService.saveOrUpdate(sysConfig,keyValue);
        return Result.succeed("保存成功");
    }
}
